package com.ruoyi.web.controller.V2.web.conntroller.apiSms;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.V2.inter.mapper.SalePartnerMapper;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.domain.TelManage;
import com.ruoyi.system.outto.goods.impl.ApiSmsService;
import com.ruoyi.system.phone.domain.TelManageNew;
import com.ruoyi.system.phone.service.ITelManageNewService;
import com.ruoyi.system.telMsg.domain.TelCallBackReq;
import com.ruoyi.system.telMsg.domain.TelSendMsg;
import com.ruoyi.system.telMsg.service.ITelSendMsgService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lz
 * @version 1.0
 * @date 2022/5/10 18:34
 */
@RestController
@RequestMapping("report")
public class ApiMsgController {


    @Autowired
    ApiSmsService apiSmsService;
    @Autowired
    SalePartnerMapper salePartnerMapper;
    @Autowired
    private ITelSendMsgService telSendMsgService;
    @Autowired
    private ITelManageNewService telManageNewService;

    @PostMapping("/phone/platform")
    public AjaxResult platform(@RequestBody TelCallBackReq req) {
        System.out.println("-----------------------------------------------");
        System.out.println("    -------------卡位同步--------------------");
        System.out.println("-----------------------------------------------");
        System.out.println(JSONObject.toJSONString(req));
        String phone = req.getN();
        TelManageNew manage = new TelManageNew();
        manage.setTel(phone);
        List<TelManageNew> telManages = telManageNewService.selectTelManageNewList(manage);
        // todo 智能卡位识别失败 不自动添加
        if (CollectionUtils.isEmpty(telManages)) {

            manage.setCreateId(1L);
            manage.setTel(phone);
            manage.setType("手机卡");
            manage.setNum(req.getCusId());
            manage.setRemark("接码平台自动添加");
            manage.setStatus(1L);
//            manage.setTelType(2);
//            if (req.getSts() == 1) {
//                manage.setTelStatus(1);
//            } else {
//                manage.setTelStatus(2);
//            }
//            manage.setTelMsg(req.getSpend());

//            telManageService.insertTelManage(manage);
            return AjaxResult.success();
        }
        TelManageNew telManage = telManages.get(0);

//        telManage.setTelType(2);
//        if (req.getSts() == 1) {
//            telManage.setTelStatus(1);
//        } else {
//            telManage.setTelStatus(2);
//        }
//        telManage.setTelMsg(req.getSpend());
//        telManageService.updateTelManage(telManage);
        return AjaxResult.success();
    }
    /**
     * 过盼之的验证码
     */
    @PostMapping("/msg")
    public AjaxResult panzhi(@RequestBody TelCallBackReq req) {
        TelSendMsg msg = new TelSendMsg();
        msg.setCreateId(1L);
        msg.setCreateName("系统");
        msg.setN(req.getFrom());
        msg.setTo(req.getN());
        msg.setTxt(req.getTxt());
        msg.setType("2");
        msg.setCode("200");

        TelManageNew aNew = new TelManageNew();
        aNew.setTel(req.getN());
        List<TelManageNew> manageNews = telManageNewService.selectTelManageNewList(aNew);
        if (CollectionUtils.isNotEmpty(manageNews)){
            msg.setCategory(manageNews.get(0).getCategory());
        }
        telSendMsgService.insertTelSendMsg(msg);



        String phone = req.getN();
        String msg1 = req.getTxt();
        System.out.println("msg=" + msg);
        System.out.println("phone=" + phone);
        if (msg1.contains("盼之代售") && msg1.contains("打死不要")) {
            List<SalePartner> salePartners = salePartnerMapper.selectSalePartnerByPhone(phone);
            if (CollectionUtils.isNotEmpty(salePartners)) {
                SalePartner salePartner = salePartners.get(0);
                apiSmsService.toPanzhi(msg1, salePartner);
            }
        }
        return AjaxResult.success();
    }

}
